package com.ztom.top100;

/**
 * 移动零
 * <p>
 * https://leetcode-cn.com/problems/move-zeroes/
 *
 * @author ZhangTao
 */
public class Code73MoveZeroes {

    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }

        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        while (index < nums.length) {
            nums[index++] = 0;
        }
    }

    public void moveZeroes1(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }

        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index] = nums[i];
                if (index++ != i) {
                    nums[i] = 0;
                }
            }
        }
    }
}
